package com.totoro.utils;

import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author TOTORO
 * @date 2021/5/1 17:07
 * 自创工具类，包含该项目需要的模块
 */
@UtilityClass
public class TotoroUtils {
    /**
     * 生成随机数字指定位数
     *
     * @param length [生成随机数的长度]
     * @return
     */
    public static String getRandomNickname(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }

    /**
     * 获取随机16进制颜色
     * @return
     */
    public static String getColor(){
        StringBuffer color = new StringBuffer("#");
        String[] letter = new String[]{"A","B","C","D","E","F"};
        // 循环六次
        for (int i = 0; i < 6; i++) {
            // 使用随机数进行字母数字判断
            int flag = Integer.parseInt(getRandomNickname(1));
            if(flag<7){
                color.append(getRandomNickname(1));
            } else{
                color.append(letter[new Random().nextInt(5)]);
            }
        }
        return color.toString();
    }

    /**
     * 获取混搭随机数
     * @param length
     * @return
     */
    public static String getRandomChar(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuffer sb = new StringBuffer("");
        for(int i=0;i<length;i++){
            int random=(int)(Math.random()*str.length());
            sb.append(str.charAt(random));
        }
        return sb.toString();
    }

    /**
     * 获得服务器地址
     * @return
     */
    public static String getServerAddress(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getScheme()+"://"+request.getServerName();
    }

    /**
     * 将字符串中的数字提取
     * @param str
     * @return
     */
    public static String getStrIncludeNumber(String str){
        String regex ="\\d*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        StringBuffer numberStr = new StringBuffer("");
        while (m.find()) {
            if (!"".equals(m.group())) {
                numberStr.append(m.group());
            }
        }
        return numberStr.toString();
    }

    /**
     * 指定时间师傅穿是否在某个月
     * @param date
     * @param month
     * @return
     */
    @SneakyThrows
    public static boolean isThisYearMonth(String date, int month){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal=Calendar.getInstance();
        //得到前某个月毫秒数
        cal.add(Calendar.MONTH, -month);
        long afterDate = cal.getTimeInMillis();
        // 得到指定时间的毫秒数
        long nowDate = simpleDateFormat.parse(date).getTime();
        if(nowDate > afterDate){
            return true;
        }
        return false;
    }

    /**
     * 指定时间字符串是否在这周
     * @param date
     * @return
     */
    @SneakyThrows
    public static boolean isThisWeek(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(simpleDateFormat.parse(date).getTime()));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        System.out.println(paramWeek +"=="+ (currentWeek-1));
        if (paramWeek == (currentWeek-1)) {
            return true;
        }
        return false;
    }

    /**
     * 数组去重
     * @param arr
     * @return
     */
    public static Object[] ArrayDeDuplication(Object[] arr){
        LinkedHashSet<Object> temp = new LinkedHashSet<>();
        for(int i=0;i<arr.length;i++){
            temp.add(arr);
        }
        return temp.toArray();
    }

    /**
     * 正则表达式过滤<img > 标签
     * @param str
     * @return
     */
    public static String cutOutImgPrefix(String str, String imgReplaceStr){
        String regex = "<img[^>]*>";
        return str.replaceAll(regex, imgReplaceStr);
    }
}
